<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="工人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName">
                <a-input v-model="model.workerName" disabled></a-input>
              </a-form-model-item>
            </a-col>
            <!--<a-col :span="24">
              <a-form-model-item label="工种" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchasePrice">
                <a-input v-model="model.workerType" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>-->
            <a-col :span="24">
              <a-form-model-item label="劳务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amountPaid">
                <a-input :value="getLabourTypeName(model.labourType)" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectAddress">
                <a-input v-model="model.projectAddress" placeholder="请输入项目地址" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="付款账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentAccountId">
                <a-select
                  show-search
                  :allowClear="true"
                  placeholder="请选择付款账户"
                  option-filter-prop="children"
                  v-model="model.paymentAccountId"
                  style="width: 100%"
                  :filter-option="filterOptionAccount"
                  @change="changeAccount">
                  <a-select-option :value="item.id" v-for="(item,index) in accountData" :key="index">
                    {{item.accountName}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="劳务金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remitMoney">
                <a-input-number v-model="model.labourMoney" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="待付金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMoney">
                <a-input-number v-model="model.toBePaidMoney" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMoney">
                <a-input-number v-model="model.paymentMoney" style="width: 100%"/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="付款凭证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentVoucher">
                <JImageUpload v-model="model.paymentVoucher"></JImageUpload>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <a-button type="primary" @click="handleOk" style="width: 100%;">保存</a-button>
  </a-drawer>

</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import {validateDuplicateValue} from "../../../../../utils/util";
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import Vue from "vue";
  import {filterDictTextByCache} from '@comp/dict/JDictSelectUtil'

  export default {
    name: 'labourPendingPaymentModal',
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        title:'',
        width:'100%',
        visible: false,
        disableSubmit: false,
        uploadGoOn:true,
        accountData:[],
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          paymentAccountId: [{ required: true, message: '付款账户不能为空' }],
          paymentVoucher: [{required: true, message: '请上传付款凭证!'}],
        },
        url: {
          edit: "/ghFinanceLabourPendingPayment/edit",
          queryAccountList: "/ghFinanceAccount/queryAllAccountList",
        }
      }
    },
    created() {
      this.initAccountData();
    },
    methods: {
      add (record) {
        delete record.paymentAccountId;
        this.model = Object.assign({}, record);
        this.model.paymentVoucher = "";
        this.model.paymentMoney = this.model.toBePaidMoney
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.model.paymentVoucher = ""
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if (this.model.paymentMoney > this.model.labourMoney){
              this.$message.error("付款金额不能大于劳务金额");
              return;
            }
            if (this.model.paymentMoney == 0){
              this.$message.error("付款金额不能为0");
              return;
            }
            if (this.model.paymentVoucher == null || this.model.paymentVoucher === ""){
              this.$message.error("请上传付款凭证");
              return;
            }
            that.confirmLoading = true;
            let httpurl = this.url.edit;
            let method = 'post';

            //this.model.paymentStatus = 0;

            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.close();
            })
          }

        })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      initAccountData(){
        getAction(this.url.queryAccountList).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.accountData = res.result.records || res.result;
          }
        })
      },
      filterOptionAccount(input,option){
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeAccount(value){

      },
      getLabourTypeName(value){
        return filterDictTextByCache('customer_labour_distribute_labour_type', value);
      },
      /*validatePaymentVoucher(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(value === ""){
            callback("请上传付款凭证")
          }
        }
      },*/
    }
  }
</script>